<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检测类型" prop="detectType">
        <el-select v-model="queryParams.detectType" placeholder="请选择检测类型" clearable>
          <el-option
            v-for="dict in dict.type.detect_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建者" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:historyInfo:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:historyInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:historyInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="historyInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="历史信息ID" align="center" prop="historyInfoId" v-if="false"/>
      <el-table-column label="检测类型" align="center" prop=" detectType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.detect_type" :value="scope.row. detectType"/>
        </template>
      </el-table-column>
      <el-table-column label="提交图片" align="center" prop="inputData" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.inputData" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" align="center" prop="detectRes" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.detectRes" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <el-popover placement="top" trigger="hover">
            <p>{{ scope.row.createTime }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:historyInfo:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:historyInfo:remove']"
          >删除</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" >
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon-download"></i>下载
                </span>
            <el-dropdown-menu slot="dropdown" style="width: 115px">

              <el-dropdown-item command="dowloadzip" icon="el-icon-folder">全部打包下载
              </el-dropdown-item>
              <el-dropdown-item command="dowloadres" icon="el-icon-picture-outline">下载检测结果
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改历史信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检测类型" prop=" detectType">
          <el-select v-model="form.detectType" placeholder="请选择检测类型" :disabled="true">
            <el-option
              v-for="dict in dict.type.detect_type"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提交图片">
          <div v-for="(data,index) in this.inputImageList" style="display: inline-block" class="ml0">
            <el-image
              style="width: 146px; height: 146px;border-radius:4px"
              :src="data.url">
            </el-image>
          </div>
        </el-form-item>
        <el-form-item label="检测结果">
          <div v-for="(data,index) in this.imageList" style="display: inline-block" class="ml0">
            <el-image
              style="width: 146px; height: 146px;border-radius:4px"
              :src="data.url">
            </el-image>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHistoryInfo, getHistoryInfo, delHistoryInfo, addHistoryInfo, updateHistoryInfo } from "@/api/system/historyInfo";

export default {
  name: "HistoryInfo",
  dicts: ['detect_type'],
  data() {
    return {
      //提交的图片List
      inputImageList:[],
      //检测结果的图片List
      imageList:[],
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史信息表格数据
      historyInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 检测结果时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        detectType: null,
        createTime: null,
        createName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 下载操作触发
    handleCommand(command, row) {
      switch (command) {
        case "dowloadzip":
          this.DownloadZip(row);
          break;
        case "dowloadres":
          this.DownloadRes(row);
          break;
        default:
          break;
      }
    },
    //下载zip包
    DownloadZip(row){
      let fileList=[]
      let inputList=row.inputData.split(",")
      inputList.forEach((item)=>{
        fileList.push({url:item})
      })
      fileList.push({url:row.detectRes})
      this.$download.downloadZip(fileList)
    },
    //下载检测结果
    DownloadRes(row) {
      this.$download.resource(row.detectRes);
    },
    initInput(val){
      if (val) {
        // 首先将值转为数组
        const list =val.split(',');
        // 然后将数组转为对象数组
        this.inputImageList = list.map(item => {
          if (typeof item === "string") {
            if (item.indexOf(this.baseUrl) === -1) {
              item = { name: this.baseUrl + item, url: this.baseUrl + item };
            } else {
              item = { name: item, url: item };
            }
          }
          return item;
        });
      } else {
        this.inputImageList = [];
        return [];
      }
    },
    initImage(val){
      if (val) {
        // 首先将值转为数组
        const list =val.split(',');
        // 然后将数组转为对象数组
        this.imageList = list.map(item => {
          if (typeof item === "string") {
            if (item.indexOf(this.baseUrl) === -1) {
              item = { name: this.baseUrl + item, url: this.baseUrl + item };
            } else {
              item = { name: item, url: item };
            }
          }
          return item;
        });
      } else {
        this.imageList = [];
        return [];
      }
    },
    /** 查询历史信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listHistoryInfo(this.queryParams).then(response => {
        this.historyInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        historyInfoId: null,
         detectType: null,
        detectRes: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.historyInfoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加历史信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const historyInfoId = row.historyInfoId || this.ids
      getHistoryInfo(historyInfoId).then(response => {
        this.form = response.data;
        this.initImage(this.form.detectRes)
        this.initInput(this.form.inputData)
        this.open = true;
        this.title = "查看历史信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.historyInfoId != null) {
            updateHistoryInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistoryInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const historyInfoIds = row.historyInfoId || this.ids;
      this.$modal.confirm('是否确认删除历史信息编号为"' + historyInfoIds + '"的数据项？').then(function() {
        return delHistoryInfo(historyInfoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/historyInfo/export', {
        ...this.queryParams
      }, `historyInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
